using LearnMiddleware.MiddleComponents;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<MyCustomMiddleware>();
builder.Services.AddTransient<MyCustomExceptionHandler>();

var app = builder.Build();

app.UseMiddleware<MyCustomMiddleware>();
app.UseMiddleware<MyCustomExceptionHandler>();

// Middelware #1
app.Use(async (HttpContext context, RequestDelegate next) =>
{
    throw new ApplicationException("Exception for testing");
    
    await context.Response.WriteAsync("Middleware #1:Before calling next\r\n");
    // context.Response.Headers["MyHeader"] = "My header content";
    
    await next(context);
    
    await context.Response.WriteAsync("Middleware #1:After calling next\r\n");
    
});



// Middleware #5 
app.Map("/employees", (appBuilder) =>
{
    appBuilder.Use(async (HttpContext context, RequestDelegate next) =>
    {
        await context.Response.WriteAsync("Middleware #5:Before calling next\r\n");

        await next(context);
    
        await context.Response.WriteAsync("Middleware #5:After calling next\r\n");
    });
    
    appBuilder.Use(async (HttpContext context, RequestDelegate next) =>
    {
        await context.Response.WriteAsync("Middleware #6:Before calling next\r\n");

        await next(context);
    
        await context.Response.WriteAsync("Middleware #6:After calling next\r\n");
    });
});

// Middleware  MapWhen
app.MapWhen((context) =>
{
    return context.Request.Path.StartsWithSegments("/employees") 
           && context.Request.Query.ContainsKey("id");
}, (appBuilder) =>
{
    appBuilder.Use(async (HttpContext context, RequestDelegate next) =>
    {
        await context.Response.WriteAsync("Middleware #7:Before calling next\r\n");

        await next(context);
    
        await context.Response.WriteAsync("Middleware #7:After calling next\r\n");
    });
});

// Middleware UseWhen
app.UseWhen((context) =>
{
    return context.Request.Path.StartsWithSegments("/employees") 
           && context.Request.Query.ContainsKey("id");
}, (appBuilder) =>
{
    appBuilder.Use(async (HttpContext context, RequestDelegate next) =>
    {
        await context.Response.WriteAsync("Middleware #8:Before calling next\r\n");

        await next(context);
    
        await context.Response.WriteAsync("Middleware #8:After calling next\r\n");
    });
});

// Middelware #2
app.Use(async (HttpContext context, RequestDelegate next) =>
{
    await context.Response.WriteAsync("Middleware #2:Before calling next\r\n");

    await next(context);
    
    await context.Response.WriteAsync("Middleware #2:After calling next\r\n");
});



// Middelware #4
// app.Run(async (context) =>
// {
//     await context.Response.WriteAsync("Middleware #4:Processed\r\n");
// });

// Middelware #3
app.Use(async (HttpContext context, RequestDelegate next) =>
{
    await context.Response.WriteAsync("Middleware #3:Before calling next\r\n");

    await next(context);
    
    await context.Response.WriteAsync("Middleware #3:After calling next\r\n");

});




app.Run();